package
{
import flash.events.*;
import flash.display.*;
import flash.geom.*;

public class UnitTest extends MovieClip
{
	
	private var g:Graphics;
	
	public function UnitTest()
	{
		g=this.graphics;
		this.addEventListener(Event.ENTER_FRAME,enterHandler);
		setDragable(apple);
		setDragable(apple2);
		setDragable(apple3);
		
		
		var rect:Rectangle = new Rectangle(200, 200, 100,100);
		var bitmapData:BitmapData=new BitmapData(500,500,false,0x000000);
		bitmapData.fillRect(rect, 0x000000);
		bitmapData.draw(this);
		bitmapData.floodFill(mouseX,mouseY, 0x000000);
		
		var bm:Bitmap=new Bitmap(bitmapData);
	
		addChild(bm);
		
	}
	
	public function setDragable(mc:MovieClip):void{
		mc.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
		mc.addEventListener(MouseEvent.MOUSE_UP,upHandler);
	}
	
	public function upHandler(evt:MouseEvent):void{
		evt.currentTarget.stopDrag();
	}
	
	public function downHandler(evt:MouseEvent):void{
		evt.currentTarget.startDrag();
	}
	
	public function enterHandler(evt:Event):void{
		
		
		
		var apple_rel:Relative=new Relative(apple.x,apple.y);
		var apple2_rel:Relative=new Relative(apple2.x,apple2.y);
		var apple3_rel:Relative=new Relative(apple3.x,apple3.y);
		
		var mp:Object=new Relative(mouseX,mouseY);
		
		
		//here read data and arrange links accordingly
		
		apple_rel.addLink(apple2_rel);
		apple2_rel.addLink(apple3_rel);
		apple3_rel.addLink(apple_rel);
		
		head=apple_rel;
		
		//g.clear();
		//g.lineStyle(1,0x000000,1);
		//drawLink(g,apple_rel);
		g.clear();
		g.lineStyle(1,0x000000,0);
		g.beginFill(0x000000,0.3);
		g.moveTo(apple_rel.x,apple_rel.y);
		drawMesh(g,apple_rel);
		g.endFill();
		
		output_txt.text=apple_rel.outputLinks();
		
		//output_txt.text=""+getDegree({x:apple.x,y:apple.y},getMouse())
		
	}
	
	private var head:Relative;
	
	public function drawMesh(g:Graphics,rel:Relative):void{
		
		var itr:Iterator=new Iterator(rel.link.toArray());
		var item:Relative;

		while(itr.hasNext()){
			item=itr.next() as Relative;
			g.lineTo(rel.x,rel.y);
			if(head!=item){
				drawMesh(g,item);
			}else{
				g.lineTo(head.x,head.y);
			}
		}
		
		
	}
	
	public function drawLink(g:Graphics,rel:Relative):void{
		var itr:Iterator=new Iterator(rel.link.toArray());
		var item:Relative;
		
		while(itr.hasNext()){
			item=itr.next() as Relative;
			lineTo(g,rel,item);
			
			if(head!=item){
				drawLink(g,item);
			}
		}
		
	}
	
	public function lineTo(g:Graphics,item:Object,target:Object):void{
		var obj:Object={x:item.x,y:item.y};
		var shorten:Object=shortenStart(obj,getDegree(obj,{x:target.x,y:target.y}));
		var shorten2:Object=shortenStart({x:target.x,y:target.y},getDegree({x:target.x,y:target.y},obj));
		g.moveTo(shorten.x,shorten.y);
		g.lineTo(shorten2.x,shorten2.y);
		
	}
	
	//move the point to the degree
	public function shortenStart(pos:Object,degree:Number):Object{
		var rad:Number=degree*Math.PI/180;
		var r:Number=20;
		var tx:Number;
		var ty:Number;
		
		tx=pos.x+r*Math.cos(rad);
		ty=pos.y-r*Math.sin(rad);
		
		return {x:tx,y:ty};
	}
	
	public function getMouse():Object{
		return {x:mouseX,y:mouseY};
	}
	
	public function getDegree(rel:Object,rel2:Object):Number{
		var x1:Number=rel.x;
		var x2:Number=rel2.x;
		var y1:Number=rel.y;
		var y2:Number=rel2.y;
		
		y1*=-1;
		y2*=-1;
		
		var angle:Number = Math.atan2(y2 - y1, x2 - x1) * (180 / Math.PI);
		return angle < 0 ? angle + 360 : angle;
	}
}

}

